package cn.zxh.csdn_maven.work.flow.download.chains;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import cn.zxh.csdn_maven.base.util.GetHtmlFromUrl;
import cn.zxh.csdn_maven.work.flow.download.AbstractFlowController;
import org.zxhtom.utils.HttpClientUtil;

@SuppressWarnings(value={"unused"})
public class FirstUrlFlow extends AbstractFlowController{

	@Override
	public Map<String, Object> execute(HttpClient client, Map<String, Object> params)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		/**获取下载地址*/
		String download_url = params.get("download_url").toString();
		/**页面构造完成。开始访问*/
		/**请求头和请求地址设置*/
		Map<String, String> headsMap=new HashMap<String, String>();
		headsMap.put("base_url", download_url);
		headsMap.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0");
		headsMap.put("Host", "download.csdn.net");
		String html = GetHtmlFromUrl.getInstance().getHtml(headsMap);
		Document document = Jsoup.parse(html);
		String data_id = document.select("div.dl_download").attr("data-id");
		String up_date = document.select("div.dl_b>span").text();
		String suffix_data="2017-12-05";
		String regex="(\\d+-\\d+-\\d+).*";
		Pattern compile = Pattern.compile(regex);
		Matcher m = compile.matcher(up_date);
		if(m.find()){
			suffix_data = m.group(1);
		}
		//去掉-
		suffix_data = Pattern.compile("-").matcher(suffix_data).replaceAll("");
		params.put("download_url", getBaseUrl()+data_id);
		result.put("suffix_data",suffix_data);
		result.put("msg","success");
		return result;
	}

	@Override
	public String getBaseUrl() {
//		dlUrl = "/index.php/source/before_do_download/" + $(".dl_download").attr("data-id")
		return "http://download.csdn.net/index.php/source/before_do_download/";
	}

}
